package com.pxmeta.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pxmeta.circle.entity.Circle;
import com.pxmeta.circle.entity.dto.CircleDTO;
import com.pxmeta.circle.service.ICircleService;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "圈子管理",tags = "圈子")
@CrossOrigin
@RequestMapping(value = "/circle")
@RequiredArgsConstructor
public class CircleController {
    private final ICircleService circleService;

    @ApiOperation(value = "查询圈子列表")
    @GetMapping("/getList")
    public AjaxResult<List<Circle>> getList(@RequestParam Integer page,
                              @RequestParam Integer size,
                              @RequestParam Integer type,
                              @RequestParam String keyword){
        IPage<Circle> result = circleService.getList(page,size,type,keyword);
        return AjaxResult.successPage(result.getRecords(),result.getTotal());
    }


    @ApiOperation(value = "根据id删除圈子")
    @DeleteMapping("/deleteById/{id}")
    public AjaxResult<Void> deleteById(@PathVariable Long id){
        circleService.removeById(id);
        return AjaxResult.success();
    }



    @ApiOperation(value = "根据id删除圈子")
    @PutMapping("/update")
    public AjaxResult<Void> update(@RequestBody Circle circle){
        Circle update = new Circle();
        update.setId(circle.getId());
        if (StringUtils.isNotBlank(circle.getName())){
            update.setName(circle.getName());
        }
        if (StringUtils.isNotBlank(circle.getAvatar())){
            update.setAvatar(circle.getAvatar());
        }
        if (StringUtils.isNotBlank(circle.getBgUrl())){
            update.setBgUrl(circle.getBgUrl());
        }
        circleService.updateById(update);
        return AjaxResult.success();
    }


}
